// SendMessage.java - Sample application.
//
// This application shows you the basic procedure for sending messages.
// You will find how to send synchronous and asynchronous messages.
//
// For asynchronous dispatch, the example application sets a callback
// notification, to see what's happened with messages.

package com.es.util.msg;

import java.util.ArrayList;
import java.util.HashMap;

import org.smslib.IOutboundMessageNotification;
import org.smslib.OutboundMessage;
import org.smslib.Service;
import org.smslib.Message.MessageEncodings;
import org.smslib.modem.SerialModemGateway;

public class SendMessage{
	
	private  Service  srv = new Service();
	//0停止状态，1启动
	private  int  srvStatus = 0;
	
	private final static SendMessage instance = new SendMessage();

	public static SendMessage getInstance() {
		return instance;
	}
	private SendMessage(){
		
	}
	public synchronized ArrayList<HashMap<String,String>> doIt(ArrayList<HashMap<String,String>> sendMsg){
		Service srv;
		ArrayList<HashMap<String,String>> list = new ArrayList<HashMap<String,String>>();
		OutboundMessage msg;
		OutboundNotification outboundNotification = new OutboundNotification();
		/*System.out.println("Example: Send message from a serial gsm modem.");
		System.out.println(Library.getLibraryDescription());
		System.out.println("Version: " + Library.getLibraryVersion());*/
		srv = new Service();
		
		SerialModemGateway gateway = new SerialModemGateway("modem.com1", "COM1", 9600, "wavecom", "17254");//115200�ǲ����ʣ�һ��Ϊ9600������ͨ���ն˲��Գ�4
		gateway.setInbound(true);
		gateway.setOutbound(true);
		gateway.setSimPin("0000");
		gateway.setOutboundNotification(outboundNotification);
		srv.addGateway(gateway);
		
		try {
			srv.startService();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		/*System.out.println("Modem Information:");
		System.out.println("  Manufacturer: " + gateway.getManufacturer());
		System.out.println("  Model: " + gateway.getModel());
		System.out.println("  Serial No: " + gateway.getSerialNo());
		System.out.println("  SIM IMSI: " + gateway.getImsi());
		System.out.println("  Signal Level: " + gateway.getSignalLevel() + "%");
		System.out.println("  Battery Level: " + gateway.getBatteryLevel() + "%");
		System.out.println();*/
		// Send a message synchronously.
		for(HashMap<String,String> map : sendMsg){
        	try {
				if(map.get("real_salary")!=null){
					msg = new OutboundMessage(map.get("emp_phone_mobile"),map.get("real_salary"));
					msg.setEncoding(MessageEncodings.ENCUCS2);//��仰
					map.put("content", map.get("real_salary"));	
					map.put("msg_result", Boolean.toString(srv.sendMessage(msg)));					
					list.add(map);
				}
			} catch (Exception e) {
			}
        }		
		
		/*System.out.println(msg);
		System.out.println("Now Sleeping - Hit <enter> to terminate.");
		System.in.read();*/
		/*msg = new OutboundMessage("18911588707","test1");
		msg.setEncoding(MessageEncodings.ENCUCS2);//��仰�Ƿ����Ķ��ű����
		srv.sendMessage(msg);*/
		try {
			srv.stopService();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
	}

	public class OutboundNotification implements IOutboundMessageNotification
	{
		public void process(String gatewayId, OutboundMessage msg)
		{
			System.out.println("Outbound handler called from Gateway: " + gatewayId);
			System.out.println(msg);
		}
	}

	public static void main(String args[])
	{
		SendMessage app = new SendMessage();
		try
		{
			/*app.doIt();*/
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}
}
